package com.binance.notification.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.binance.master.error.BusinessException;
import com.binance.master.models.APIRequest;
import com.binance.master.models.APIResponse;
import com.binance.notification.api.AlertPriceApi;
import com.binance.notification.api.request.AlertPriceQueryRequest;
import com.binance.notification.api.request.AlertPriceRequest;
import com.binance.notification.api.request.DeleteAlertPriceRequest;
import com.binance.notification.api.request.LoadAlertPriceRequest;
import com.binance.notification.api.response.AlertPriceQueryResponse;
import com.binance.notification.api.response.AlertPriceResponse;
import com.binance.notification.api.vo.AlertPriceVo;
import com.binance.notification.data.models.AlertPrice;
import com.binance.notification.web.service.IAlertPriceService;
import com.google.common.collect.Lists;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class AlertPriceController implements AlertPriceApi {

    @Resource
    private IAlertPriceService alertPrice;

    @Override
    public APIResponse<AlertPriceResponse> insert(@RequestBody @Validated APIRequest<AlertPriceRequest> request) {
        try {
            AlertPriceRequest body = request.getBody();
            AlertPrice target = new AlertPrice();
            BeanUtils.copyProperties(body, target);
            this.alertPrice.insert(target);
            AlertPriceResponse response = new AlertPriceResponse();
            BeanUtils.copyProperties(target, response);
            return APIResponse.getOKJsonResult(response);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                log.warn("插入价格提醒设置信息出错", e);
                throw (BusinessException) e;
            }
            log.error("插入价格提醒设置信息出错", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public APIResponse<List<AlertPriceResponse>> load(
            @RequestBody @Validated APIRequest<LoadAlertPriceRequest> request) {
        try {
            List<AlertPrice> list = this.alertPrice.loadByUserId(request.getBody().getUserId());
            List<AlertPriceResponse> responses = Lists.newArrayList();
            for (AlertPrice ap : list) {
                AlertPriceResponse apr = new AlertPriceResponse();
                BeanUtils.copyProperties(ap, apr);
                responses.add(apr);
            }
            return APIResponse.getOKJsonResult(responses);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                log.warn("根据用户ID查询价格提醒数据出错", e);
                throw (BusinessException) e;
            }
            log.error("根据用户ID查询价格提醒数据出错", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public APIResponse<Void> delete(@RequestBody @Validated APIRequest<DeleteAlertPriceRequest> request) {
        try {
            this.alertPrice.remove(request.getBody().getIdList(), request.getBody().getUserId());
            return APIResponse.getOKJsonResult(null);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                log.warn("根据用户ID和主键ID删除价格提醒数据出错", e);
                throw (BusinessException) e;
            }
            log.error("根据用户ID和主键ID删除价格提醒数据出错", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public APIResponse<AlertPriceQueryResponse> query(@RequestBody APIRequest<AlertPriceQueryRequest> request) {
        // TODO Auto-generated method stub
        
        AlertPriceQueryRequest alertPriceReq = request.getBody();
        log.info("alert price query alertPriceReq : {}",JSON.toJSONString(alertPriceReq));
        AlertPrice alertPrice = new AlertPrice();
        BeanUtils.copyProperties(alertPriceReq, alertPrice);
        log.info("alert price query alertPrice : {}", JSON.toJSONString(alertPrice));
        try { 
            
            List<AlertPrice> alertPriceList = this.alertPrice.query(alertPrice);
            List<AlertPriceVo> alertPriceVoList = Lists.newArrayList();
            for(AlertPrice price : alertPriceList) {
                AlertPriceVo alertPriceVo = new AlertPriceVo();
                BeanUtils.copyProperties(price, alertPriceVo);
                alertPriceVoList.add(alertPriceVo);
            }
            Long size = this.alertPrice.count(alertPrice);
            AlertPriceQueryResponse response = new AlertPriceQueryResponse();
            response.setTotal(size);
            response.setAlertPriceList(alertPriceVoList);
            return APIResponse.getOKJsonResult(response);
        }catch (Exception e) {
            if (e instanceof BusinessException) {
                log.error("query alert price failed", e);
                throw (BusinessException) e;
            }
            log.error("query alert price exception", e);
            throw new RuntimeException(e);
        }
    }

}
